#include "util.h"
#include "isr.h"
#include "print.h"

isr_t interrupt_handlers[256] = { 0 };

void isr_handler(regs *r)
{
	unsigned char int_no = r->int_no & 0xFF;
	if(interrupt_handlers[int_no] != 0)
	{
		isr_t handler = interrupt_handlers[int_no];
		handler(r);
	}
	else
	{
		printf("received interrupt: %d\n", int_no);
		printf("handler: %x\n", interrupt_handlers[int_no]);				
	}
}

void irq_handler(regs *r)
{
	unsigned char int_no = r->int_no & 0xFF;
	// send EOI to slave if necessary
	if(int_no >= 40)
	{
		outportb(0xA0, 0x20);
	}
	
	// send EOI to master
	outportb(0x20, 0x20);
	
	if(interrupt_handlers[int_no] != 0)
	{
		isr_t handler = interrupt_handlers[int_no];
		handler(r);
	}
}

void register_interrupt_handler(unsigned char n, isr_t handler)
{
	interrupt_handlers[n] = handler;
}

